package com.ymatou.iapi.couponmanage.testcase;

import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.couponmanage.parameter.CouponInfo;
import com.ymatou.iapi.couponmanage.parameter.GetCouponInfoesByCodeBean;
import com.ymatou.iapi.couponmanage.service.GetCouponInfoesByCodeCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.database.model.coupon;
import com.ymttest.database.model.couponbatch;
import com.ymttest.database.sqlwapper.couponWapper;
import com.ymttest.database.sqlwapper.couponbatchWapper;
//import com.ymttest.database.model.coupon;
//import com.ymttest.database.model.couponbatch;
//import com.ymttest.database.model.couponPrivateUserBound;
//import com.ymttest.database.model.couponValue;
//import com.ymttest.database.sqlwapper.couponbatchWapper;
//import com.ymttest.database.sqlwapper.couponPrivateUserBoundWapper;
//import com.ymttest.database.sqlwapper.couponValueWapper;
//import com.ymttest.database.sqlwapper.couponWapper;
import com.ymttest.utils.TestDataManager;
/**
*根据优惠券编号获取优惠券信息
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetCouponInfoesByCode{
	private static GetCouponInfoesByCodeBean getcouponinfoesbycodeBean;
	private static GetCouponInfoesByCodeCall getcouponinfoesbycodeCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据优惠券编号获取优惠券信息");
	}

	@Before
	public void caseUp() {
		getcouponinfoesbycodeBean=new GetCouponInfoesByCodeBean();
		getcouponinfoesbycodeCall=new GetCouponInfoesByCodeCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getcouponinfoesbycodeCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetCouponInfoesByCode_001() {
		Logger.start(true,"根据优惠券编号获取优惠券信息");
		try {
			List<coupon> coupons = new couponWapper().selectTop5();
			
			List<String> couponCodes = new ArrayList<>();
			
			for (coupon coupon : coupons) {
				couponCodes.add(coupon.getCouponCode());
			}
			getcouponinfoesbycodeBean.setCouponCodes(couponCodes);
			
			getcouponinfoesbycodeCall.setData(getcouponinfoesbycodeBean);
			getcouponinfoesbycodeCall.callService();	
			
			Logger.verifyEquals("0", getcouponinfoesbycodeCall.getCode(), "验证返回code");
			Logger.verifyEquals("操作成功！", getcouponinfoesbycodeCall.getMessage(), "验证返回Message");

			List<CouponInfo> couponInfos = getcouponinfoesbycodeCall.getCouponInfos().getCouponInfoes();
			List<String> returnCouponCodes = new ArrayList<>();
			
			for (int i=0;i<couponInfos.size();i++) {
				CouponInfo couponInfo = couponInfos.get(i);

				returnCouponCodes.add(couponInfo.getCouponCode());
				
				coupon coupon = new couponWapper().selectBycouponCode(couponInfo.getCouponCode()).get(0);
				couponbatch batch = new couponbatchWapper().selectByPrimaryKey(coupon.getBatchId());
//				couponValue value = new couponValueWapper().selectByiCouponSettingId(batch.getiCouponSettingId()).get(0);
//				couponPrivateUserBound userBound = new couponPrivateUserBoundWapper().selectBycouponCode(couponInfo.getCouponCode()).get(0);
				
				Logger.verifyEquals(batch.getBatchId(), couponInfo.getBatchId(), "验证返回batchId");
				Logger.verifyEquals(batch.getBatchCode(), couponInfo.getBatchCode(), "验证返回batchCode");
				Logger.verifyEquals(0, couponInfo.getOrderMinAmount().compareTo(batch.getMinOrderValue()), "验证返回orderMinAmount");
				Logger.verifyEquals(0, couponInfo.getCouponAmount().compareTo(batch.getCouponValue()), "验证返回couponAmount");
				
				// JIRACONN-11615
				int usedTime=coupon.getIsUsed()==true?0:1;
				Logger.verifyEquals(usedTime, couponInfo.getRemainUseTimes(), "验证返回remainUsedTimes");

			}
			Logger.verifyEquals(true,TestDataManager.isListEqual(returnCouponCodes, couponCodes), "验证返回CouponCode");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetCouponInfoesByCode_002() {
		Logger.start(true,"根据优惠券编号获取优惠券信息couponcodes空");
		try {
			
			getcouponinfoesbycodeCall.setData(getcouponinfoesbycodeBean);
			getcouponinfoesbycodeCall.callService();	
			
			Logger.verifyEquals("0", getcouponinfoesbycodeCall.getCode(), "验证返回code");
			Logger.verifyEquals("操作成功！", getcouponinfoesbycodeCall.getMessage(), "验证返回Message");

			List<CouponInfo> couponInfos = getcouponinfoesbycodeCall.getCouponInfos().getCouponInfoes();
			
			Logger.verifyEquals(0, couponInfos.size(), "验证返回Message");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetCouponInfoesByCode_003() {
		Logger.start(true,"根据优惠券编号获取优惠券信息couponcodes不存在");
		try {
			
			List<String> couponCodes = new ArrayList<>();
			couponCodes.add("1111");
			getcouponinfoesbycodeBean.setCouponCodes(couponCodes);
			getcouponinfoesbycodeCall.setData(getcouponinfoesbycodeBean);
			getcouponinfoesbycodeCall.callService();	
			
			Logger.verifyEquals("0", getcouponinfoesbycodeCall.getCode(), "验证返回code");
			Logger.verifyEquals("操作成功！", getcouponinfoesbycodeCall.getMessage(), "验证返回Message");

			List<CouponInfo> couponInfos = getcouponinfoesbycodeCall.getCouponInfos().getCouponInfoes();
			
			Logger.verifyEquals(0, couponInfos.size(), "验证返回Message");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}